<template>
  <div class="register">
    <Header>
      <span>注册</span>
    </Header>
    <section>
      <div class="iphone">
        <input
          type="text"
          placeholder="请输入手机号"
          pattern="[0-9]*"
          v-model="userTel"
          autocomplete="off"
        />
      </div>
      <div class="yzm">
        <div>
          <input
            type="text"
            placeholder="请输入短信验证码"
            pattern="[0-9]*"
            v-model="msgNum"
            autocomplete="off"
          />
        </div>
        <button class="btn" @click="getMsg" :disabled="disabled">
          {{ codeMsg }}
        </button>
      </div>
      <div class="pwd">
        <input
          type="password"
          placeholder="请设置密码"
          pattern="[0-9]*"
          v-model="pwdLogin"
          autocomplete="off"
        />
      </div>
      <div class="log" @click="goRegister">注 册</div>
    </section>
  </div>
</template>

<script>
import Header from "@/components/Login/Header.vue";
import { Toast } from "mint-ui";
import http from "@/common/request";
import { encrypt } from "@/common/rsaEncrypt";
export default {
  data() {
    return {
      disabled: false,
      codeMsg: "获取短信验证码",
      userTel: "",
      msgNum: "",
      pwdLogin: "",
      codeNum: 6,
      rules: {
        userTel: {
          rule: /^1[23456789]\d{9}$/,
          msg: "手机号不能为空,并且是11位数字",
        },
        pwdLogin: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空,并且是6-12位数字",
        },
      },
    };
  },
  components: { Header },
  methods: {
    // 点击注册
    async goRegister() {
      if (!this.validate("userTel")) return;
      if (!this.validate("pwdLogin")) return;
      if (this.code != this.msgNum) {
        Toast("验证码错误");
      }
      // 密码加密
      let pwd = encrypt(this.pwdLogin);
      // 只有通过手机号和密码前端验证后才发送登录请求
      let res = await http.$axios({
        url: "/api/register",
        method: "post",
        data: {
          userTel: this.userTel,
          userPwd: pwd,
        },
      });
      console.log(res);
      if (res.data.success) {
        this.$router.push({
          name: "Login",
        });
      }
    },
    // 获取短信
    async getMsg() {
      if (!this.validate("userTel")) return;
      // 发送短信
      let res = await http.$axios({
        url: "/api/code",
        method: "POST",
        data: {
          iphone: this.userTel,
        },
      });
      if (res.data.success) {
        this.code = res.data.data;
        console.log(this.code);
      }

      // 禁用按钮
      this.disabled = true;
      // 倒计时
      let time = setInterval(() => {
        --this.codeNum;
        this.codeMsg = `重新发送${this.codeNum}`;
      }, 1000);
      // 判断什么时候停止计时器
      setTimeout(() => {
        clearInterval(time);
        this.disabled = false;
        this.codeNum = 6;
        this.codeMsg = "获取短信验证码";
      }, 6000);
    },
    validate(key) {
      let bool = true;
      if (!this.rules[key].rule.test(this[key])) {
        Toast(this.rules[key].msg);
        bool = false;
        return false;
      }
      return bool;
    },
  },
};
</script>

<style scoped>
input {
  font-size: 18px;
}
.pwd {
  width: 80%;
  height: 40px;
  margin-bottom: 20px;
}
.pwd input {
  height: 100%;
  width: 100%;
  padding-left: 10px;
}
.userLogin {
  height: 100vh;
  width: 100vw;
  background-color: #eee;
}
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 40px;
}
.iphone,
.yzm {
  height: 40px;
  width: 80%;
  margin-bottom: 20px;
}
.iphone input {
  width: 100%;
  height: 100%;
  outline: none;
  padding-left: 10px;
}
.yzm {
  display: flex;
  height: 40px;
}
.yzm input {
  padding-left: 10px;
  width: 170px;
  height: 40px;
  outline: none;
}
.yzm .btn {
  box-sizing: border-box;
  height: 100%;
  flex: 1;
  font-size: 15px;
  border: 1px solid red;
  border-radius: 5px;
  text-align: center;
  line-height: 40px;
  background-color: red;
  color: antiquewhite;
  font-weight: 700;
}
.log {
  width: 80%;
  height: 40px;
  background-color: rgba(225, 0, 0, 1);
  border-radius: 10px;
  text-align: center;
  line-height: 40px;
  font-size: 18px;
  color: #eee;
}
</style>